/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jeelabs.lab2;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author bastian
 */
@FacesConverter(forClass=Cave.class)
public class CaveConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Cave a;
        EntityManager em = ((EntityManagerFactory) context.getExternalContext().getApplicationMap().get("emf")).createEntityManager();
        a = em.find(Cave.class, Integer.valueOf(value));
        em.close();

        return a;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Cave a = (Cave) value;

        return a.getId().toString();
    }
}
